<?php
namespace Rootadmin\Controller;
use Think\Controller;
class SystemController extends AdminController {
    //管理员管理
    public function admin(){
        $count=M("admin")->count();
        $Page=new \Think\Page($count,10);
        $info=M("admin")->limit($Page->firstRow.','.$Page->listRows)->order("id desc")->select();
        $this->assign("pages",$Page->show());
        $this->assign("info",$info);
        $this->display();
    }

    //添加管理员
    public function add(){
        if($_POST){
            $data["name"]=I("name","");
            $data["password"]=I("password","","md5");
            $data["ip"]=get_client_ip();
            $data["addtime"]=time();
            if(M("admin")->add($data)){
                $this->success("管理员添加成功",U('Rootadmin/system/admin'));
            }else{
                $this->error("管理员添加失败",U('Rootadmin/system/admin'));
            }
        }
        else{
            $this->display();
        }
    }
    //修改管理员信息
    public function edit(){
        $id=I("id","");
        if($_POST){
            $data["name"]=I("name","");
            if(I("password","")!=""){
                $data["password"]=I("password","","md5");
            }
            $res=M("admin")->where("id=$id")->save($data);
            if($res===false){
                $this->error("管理员修改失败",U('Rootadmin/system/admin'));
            }else{
                $this->success("管理员修改成功",U('Rootadmin/system/admin'));
            }
        }
        else{
            $info=M("admin")->where("id=$id")->find();
            $this->assign("info",$info);
            $this->display();
        }
    }
    //删除管理员
    public function delete(){
        $id=I("id","");
        if(M("admin")->where("id=$id")->delete()){
            $this->ajaxReturn(array("code"=>200,"msg"=>"管理员删除成功"));
        }else{
            $this->ajaxReturn(array("code"=>201,"msg"=>"管理员删除失败"));
        }
    }
    //公司广告列表
    public function advertisement(){
        $count=M("advertisement")->count();
        $Page=new \Think\Page($count,10);
        $info=M("advertisement")->limit($Page->firstRow.','.$Page->listRows)->order("id desc")->select();
        $this->assign("pages",$Page->show());
        $this->assign("info",$info);
        $this->display();
    }
    //添加广告
    public function add_adv(){
        if($_POST){
            $data["title"]=I("title","");
            $data["content"]=I("content","");
            $arr = I("thumbs", []);
            $data["img"]=$arr[0];
            $data["add_time"]=time();
            if(M("advertisement")->add($data)){
                $this->success("广告添加成功",U('Rootadmin/system/advertisement'));
            }else{
                $this->error("广告添加失败",U("Rootadmin/system/advertisement"));
            }
        }
        else{
            $this->display();
        }
    }
    //修改广告
    public function edit_adv(){
        $id=I("id","");
        if($_POST){
            $data["title"]=I("title","");
            $data["content"]=I("content","");
            $arr = I("thumbs", []);
            $data["img"]=$arr[0];
            $data["add_time"]=time();
            if(M("advertisement")->add($data)){
                $this->success("广告添加成功",U('Rootadmin/system/advertisement'));
            }else{
                $this->error("广告添加失败",U("Rootadmin/system/advertisement"));
            }
        }else{
            $info=M("advertisement")->where("id=$id")->find();
            $this->assign("info",$info);
            $this->display();
        }
    }
    //上传图片
    public function upload(){
        foreach ($_FILES as $v) {
            $file = $v;
        }
        $upload = imgUpload($file, "uploads/rootadmin/", "uniqid");
        echo $upload['path'];
    }
    //删除广告
    public function delete_adv(){
        $id=I("id","");
        if(M("advertisement")->where("id=$id")->delete()){
            $this->ajaxReturn(array("code"=>200,"msg"=>"广告删除成功"));
        }else{
            $this->ajaxReturn(array("code"=>201,"msg"=>"广告删除失败"));
        }
    }


}